﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.Contacts;
using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;
using System.Collections.ObjectModel;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
namespace Gmail.Model
{
    public class Connector
    {
        private Connector(String Username, String Password) {
            _Username = Username;
            _Password = Password;

        }
        private static Connector _Singleton = null;
        public static Connector GetInstance(String Username, String Password)
        {
            if (_Singleton == null)
            {
                _Singleton = new Connector(Username, Password);
            }
            else
            {
                if (_Singleton._Password != Password || _Singleton._Username != Username)
                {
                    _Singleton = new Connector(Username, Password);
                }
            }
            return _Singleton;
        }
        private String _Username;
        private String _Password;
        private Boolean _Connected = false;
        private List<EmailEntry> _EmailEntries = new List<EmailEntry>();
        public ReadOnlyCollection<EmailEntry> EmailEntires { get { return _EmailEntries.AsReadOnly(); } }
        public String Username { get { return _Username; } }
        public Boolean Connected { get { return _Connected; } }
        
        public Boolean Connect()
        {
            if (_Connected) return true;
            try
            {
                RequestSettings RS = new RequestSettings("GmailAPI", _Username, _Password);
                RS.AutoPaging = true;
                ContactsRequest CR = new ContactsRequest(RS);


                Feed<Contact> Feed = CR.GetContacts();

                foreach (Contact Contact in Feed.Entries) //Might throw InvalidCredentalsException
                {
                    foreach (EMail Email in Contact.Emails)
                    {
                        EmailEntry EmailEntry = new EmailEntry(Contact.Title, Email.Address);
                        _EmailEntries.Add(EmailEntry);
                    }
                }

                _Connected = true;
            }
            catch { }
            return _Connected;
        }

        public Boolean Send(String Email,String Filename,String DisplayFilename,String Subject, String Body)
        {
            byte[] UTF8Bytes = System.Text.Encoding.UTF8.GetBytes(DisplayFilename);
            String UTF8Filename = "=?UTF-8?B?" + Convert.ToBase64String(UTF8Bytes) + "?=";
            SmtpClient Client = new SmtpClient("smtp.gmail.com", 587);
            Client.EnableSsl = true;
            Client.Credentials = new NetworkCredential(_Username, _Password);
            Client.DeliveryMethod = SmtpDeliveryMethod.Network;

            MailMessage Message = new MailMessage();
            Message.From = new MailAddress(_Username, _Username);
            Message.To.Add(new MailAddress(Email));
            Message.Headers.Add("X-Plugin", "Gmail-Office-Plugin");
            Message.IsBodyHtml = true;
            Message.Priority = MailPriority.Normal;
            Message.Subject = Subject;
            Message.Body = Body;
            System.IO.FileStream Stream=new System.IO.FileStream(Filename,System.IO.FileMode.Open,System.IO.FileAccess.Read);
            ContentType ContentType=new ContentType("application/msword");
            ContentType.Name = UTF8Filename;
            Attachment Attachment = new Attachment(Stream, ContentType);
            Attachment.ContentDisposition.Inline = false;
            Attachment.ContentDisposition.FileName = UTF8Filename;
            Message.Attachments.Add(Attachment);
            
            try
            {
                Client.Send(Message);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }

    }
}
